package org.gthelper.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletConfig;

import org.gthelper.database.DBHandler;
import org.gthelper.hdfs.HdfsMA;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


/**
 * 在主Servlet类中初始化系统
 * @author root
 * 通过调用其静态方法初始化系统
 * 仅用于初始化基于Servlet的实现
 * 在主Servlet的init方法中调用，以避免重复调用浪费资源
 */
public class GTServletInitXML {

	/**
	 * 根据配置文件初始化系统
	 * @param config 当前servlet的congfig对象
	 * @param confPath xml形式的配置文件的路径（相对于WEB-INF所在目录）
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static void gtInit(ServletConfig config,String confPath)
	{
		SAXBuilder builder=new SAXBuilder();
		try {
			InputStream file = config.getServletContext().getResourceAsStream(confPath);
			org.jdom.Document doc=builder.build(file);
			Element root=doc.getRootElement();
			
			List<Element> hdpNn =  root.getChildren("hdp-namenode");
			if(hdpNn.size()>0)
			{
				SysInfo.hdpNamenode = hdpNn.get((hdpNn.size()-1)).getText();
			}
			
			List<Element> hdpTr =  root.getChildren("hdp-tracker");
			if(hdpTr.size()>0)
			{
				SysInfo.hdpTracker = hdpTr.get((hdpTr.size()-1)).getText();
			}
			
			List<Element> hdpFsd =  root.getChildren("hdp-fs-default");
			if(hdpFsd.size()>0)
			{
				SysInfo.hdpFsDefault = hdpFsd.get((hdpFsd.size()-1)).getText();
				HdfsMA.setUrl(SysInfo.hdpFsDefault);
			}
			
			List<Element> hrPath = root.getChildren("hdfsRootPath");
			if(hrPath.size()>0)
			{
				SysInfo.hdfsRootPath = hrPath.get((hrPath.size()-1)).getText();
				HdfsMA.setUrl(HdfsMA.getUrl()+SysInfo.hdfsRootPath);
			}
			
			List<Element> upTempPath = root.getChildren("uploadTempPath");
			if(upTempPath.size()>0)
			{
				SysInfo.upTempPath = upTempPath.get((upTempPath.size()-1)).getText();
			}
			
			List<Element> downTempPath = root.getChildren("downloadTempPath");
			if(downTempPath.size()>0)
			{
				SysInfo.downTempPath = downTempPath.get((downTempPath.size()-1)).getText();
			}
			
			List<Element> dbdriver = root.getChildren("database-driver");
			if(dbdriver.size()>0)
			{
				SysInfo.db_driver = dbdriver.get((dbdriver.size()-1)).getText();
				DBHandler.db_driver=SysInfo.db_driver;
			}
			
			List<Element> dbname = root.getChildren("database-name");
			if(dbname.size()>0)
			{
				SysInfo.db_name = dbname.get((dbname.size()-1)).getText();
				DBHandler.db_name = SysInfo.db_name;
			}
			
			List<Element> loginIndate = root.getChildren("loginIndate");
			if(loginIndate.size()>0)
			{
				SysInfo.loginIndate = Long.parseLong(loginIndate.get((loginIndate.size()-1)).getText(),10);
			}
			
			List<Element> splitpixelsize = root.getChildren("splitpixelsize");
			if(splitpixelsize.size()>0)
			{
				SysInfo.splitpixelsize = Integer.parseInt(splitpixelsize.get(splitpixelsize.size()-1).getText(),10);
			}
			
			List<Element> residule = root.getChildren("residule");
			if(residule.size()>0)
			{
				SysInfo.residule = Integer.parseInt(residule.get(residule.size()-1).getText(),10);
			}
			
		} catch (JDOMException | IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			System.out.println("读取配置文件失败");
		}
	}	
	
}
